<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2019/03/28 16:38
 * Desc: desc
 */
class Model_Interface extends ORM
{

    /**
     * 获取接口配置
     * @function
     */
    public static function get_product_interface($configid = '')
    {
        if($configid)
        {
            $config = DB::select()->from("ferry_ticket_interface")->where("id","=",$configid)->and_where("status","=","1")->execute()->current();
        }
        else
        {
            $config = DB::select()->from("ferry_ticket_interface")->where("status","=","1")->execute()->as_array();
        }

        return $config;
    }


    /**
     * array_column() // 不支持低版本;
     * 以下方法兼容PHP低版本
     */
    private static function _array_column(array $array, $column_key, $index_key=null){
        $result = [];
        foreach($array as $arr) {
            if(!is_array($arr)) continue;

            if(is_null($column_key)){
                $value = $arr;
            }else{
                $value = $arr[$column_key];
            }

            if(!is_null($index_key)){
                $key = $arr[$index_key];
                $result[$key] = $value;
            }else{
                $result[] = $value;
            }
        }
        return $result;
    }

    /**
     * 验证是否是第三方的订单
     */
    public static function check_interface_order($orderid)
    {
        $product = DB::select()->from("member_order")
            ->where("id","=",$orderid)
            ->execute()->current();

        $interfaceConfig = '';

        if($product){
            $suitid = $product['suitid'];

            $ticket = DB::select()->from("ferry_ticket")
                ->where("id","=",$suitid)
                ->execute()->current();

            if($ticket['create_type'] != 0){

                // 接口配置
                $interfaceConfig = DB::select()->from("ferry_ticket_interface")
                    ->where("id","=",$ticket['create_type'])
                    ->and_where("status","=","1")
                    ->execute()->current();

            }
        }

        return $interfaceConfig;
    }

    /**
     * 系统支付完成后第三方接口数据处理
     * @function
     * @param $ordersn
     * @throws Kohana_Exception
     */
    public static function pay_over($ordersn)
    {

        // 检查订单类型
        if($ordersn){
            $orderinfo = DB::select("typeid","suitid","status")->from("member_order")->where("ordersn","=",$ordersn)->execute()->current();

            if($orderinfo){
                $typeid = $orderinfo['typeid'];
                $status = $orderinfo['status'];
                // 如果是门票 需要处理接口
                if($typeid == 888 && $status == 2){  // 第三方预创建订单且已支付

                    $suitInfo   = DB::select("create_type")->from("ferry_ticket")->where("id","=",$orderinfo['suitid'])->execute()->current();

                    $createtype = $suitInfo['create_type'];

                    if($createtype != 0)
                    {
                        $interfaceconfig = Model_Interface::get_product_interface($createtype);
                        if(St_Functions::is_normal_app_install('supplier_xiyuyou'))
                        {
                            // 获取到第三方接口信息
                            if($interfaceconfig['pinyin'] == 'xiyuyou')
                            {
                                Model_Ferry_Xiyuyou_Interface::xiyuyou_pay($ordersn);

                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 订单取消
     * @function
     * @param $ordersn string 订单编号
     * @param $createtype  创建编号
     */
    public function order_cancel($ordersn,$createtype)
    {
        // 检查属于哪个第三方
        $interfaceconfig = Model_Interface::get_product_interface($createtype);

        // 获取到第三方接口信息
        if($interfaceconfig['pinyin'] == 'xiyuyou')
        {
            // 判断应用是否安装
            if(St_Functions::is_normal_app_install('supplier_xiyuyou'))
            {
                $cancelres = Model_Ferry_Xiyuyou_Interface::xiyuyou_cancel($ordersn);
                if(!$cancelres['status'])
                {
                    return array('status' => false, 'msg'=> $cancelres['msg']);
                }
                else
                {
                    // 西域游返回的损失 西域游金额单位为：分 需要修改重新计算
                    $cancelData  = $cancelres['data'];
                    $refund_cost = isset($cancelData['Fee']) ? bcdiv($cancelData['Fee'],100,2) : 0;
                    return array('status' => true, 'msg'=> '', 'refund_cost'=> $refund_cost);

                }
            }
            else
            {
                return array('status' => false, 'msg'=> '应用不存在或已被卸载，退款失败');
            }
        }

        return array('status' => true, 'msg'=> '', 'refund_cost'=> 0);
    }


}